import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { STChange } from '@delon/abc/st';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { RoleEditComponent } from './edit/role-edit.component';
import { RoleService } from './role.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-user-list',
  templateUrl: './role-list.component.html',
  styles: []
})
export class RoleListComponent implements OnInit {

  @ViewChild('roleEditComponent', {static: false})
  public roleEditComponent: RoleEditComponent;

  constructor(private http: _HttpClient,
              private msgSrv: NzMessageService,
              private modal: ModalHelper,
              private cdr: ChangeDetectorRef,
              private roleService: RoleService) {
  }

  url = 'api/sys-role/page-list';

  public params: any = {};

  checked = false;

  total = 1;
  size = 10;
  page = 0;
  loading = false;

  roles = [];

  setOfCheckedId = new Set<number>();

  loadRoleList() {

    this.loading = true;

    this.roleService.findPage({'size': this.size, 'page': this.page}).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.roles = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  ngOnInit(): void {
    this.loadRoleList();
  }

  change(e: STChange): void {
    console.log(e);
  }

  delete(id?) {
    this.loading = true;
    this.roleService.delete(id).subscribe((res) => {
      this.loading = false;
      if(res.code === 1){
        this.msgSrv.success('删除完成');
      }else{
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);
  }

  onAllChecked(e){

  }

  cancel() {
    this.msgSrv.success('已取消');
  }

  openEdit(record ?: any): void {

    console.log(record);
    this.roleEditComponent.edit(record);

  }

  onSmt() {
    this.loadRoleList();
  }

  _onReuseInit() {
    this.loadRoleList();
  }
}
